import { message } from "antd";
import axios from "axios";

const newInstance = axios.create({
    baseURL: "http://127.0.0.1:8002",
    timeout: 5000
})

/**
 * 请求拦截器
 */
newInstance.interceptors.request.use((req) => {
    const token = localStorage.token
    if(token){
        req.headers.token = token
    }
    return req;
})

newInstance.interceptors.response.use((resp)=>{
    //操作数据，过滤数据等等
    return resp
},(error)=>{
    const response = error.response
    if(response){
        switch(response.status){
            case 500:
                message.error("服务器开小差了")
                break;
            case 404:
                message.error("资源找不到")
                break;
            case 401:
                localStorage.removeItem("token")
                localStorage.removeItem("userInfo")
                window.location.href="/"
                break;
            default:
                message.error("程序异常退出")
        }
    }
})

export default newInstance